package nature.nano.control.script.assembly;

import nature.nano.component.EmptyProps;
import nature.nano.component.PrimaryPipe;
import nature.nano.component.Strings;
import nature.nano.iface.Props;
import nature.nano.iface.secondary.Startable;

public class ScriptAssembly extends PrimaryPipe implements Startable {
	
	private Props clazzProp;
	private Props propsProp;

	private ScriptParser parser;
	
	private ScriptParser getParser() {
		if (parser == null) {
			parser = new ScriptParser(slot(Strings.error));			
		}
		return parser;
	}
	
	@Override
	public void start() {
		slot(Strings.output).onBegin(Strings.root, EmptyProps.instance);
		getParser().start();
		slot(Strings.output).onEnd(Strings.root);
	}
	
	@Override
	public void anBegin(String type, Props props) {
		if (type.equals(Strings.row)) {
			getParser().newRow();
		}
		if (type.equals(Strings.clazz)) {
			clazzProp = props;
		}
		if (type.equals(Strings.props)) {
			propsProp = props;
		}
		slot(Strings.error).onBegin(type, props);
	}

	@Override
	public void anEnd(String type) {
		if (type.equals(Strings.clazz)) {
			getParser().addClass(clazzProp, propsProp);
			clazzProp = null;
			propsProp = null;
		}
		slot(Strings.error).onEnd(type);
	}
	
}
